﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;

namespace WebberCross.JavaScriptBridge.JSBridgeDemoApp.ViewModels
{
    [ScriptableType()]
    public class ViewModelBase
    {
        protected string _jsUniqueClientID = "";
        protected string _jsClientID = "";
        protected string _jsClientSourceID = "";

        public ViewModelBase()
        {
            // Register this object for JavaScript interaction
            if(HtmlPage.IsEnabled)
                HtmlPage.RegisterScriptableObject("JSBridgeControl", this);

            // Get JS Bridge parameters
            // Get any params passed from WebPart
            if (App.Current.Resources.Contains("jsUniqueClientID"))
                _jsUniqueClientID = App.Current.Resources["jsUniqueClientID"].ToString();

            if (App.Current.Resources.Contains("jsClientID"))
                _jsClientID = App.Current.Resources["jsClientID"].ToString();

            if (App.Current.Resources.Contains("jsClientSourceID"))
                _jsClientSourceID = App.Current.Resources["jsClientSourceID"].ToString();
        }

        /// <summary>
        /// This method is called by the JS Bridge
        /// Override it to implement your own functionality
        /// </summary>
        /// <param name="arg"></param>
        [ScriptableMember()]
        public virtual void JSBridgeReceive(string arg)
        {
            MessageBox.Show(arg);
        }

        /// <summary>
        /// Sends an object to the JS Bridge
        /// </summary>
        /// <param name="arg"></param>
        protected void JSBridgeSend(object arg)
        {
            HtmlPage.Window.Invoke("JSBridgeSend", _jsClientID, arg);
        }
    }
}
